package com.cjh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.common.dto.PermissionDTO;
import com.cjh.common.dto.PermissionDTO1;
import com.cjh.common.entity.Permission;
import com.cjh.common.entity.RolePermission;
import com.cjh.common.mapper.RoleMapper;
import com.cjh.common.mapper.RolePermissionMapper;
import com.cjh.common.service.PermissionService;
import com.cjh.common.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Override
    public List<PermissionDTO> getMenuTree(Integer id) {
        return permissionMapper.getMenuTree(id);
    }

    @Override
    public Set<String> getAllPermissionByUsername(String username) {
        return permissionMapper.selectAllPermissionByUsername(username);
    }

    @Override
    public Boolean isSuperAdministrator(String username) {
        Set<String> strings = roleMapper.selectAllRolesByUsername(username);
        return strings.contains("超级管理员");
    }

    @Override
    public List<PermissionDTO1> getMenuByUserId(Integer id, Integer permissionId) {
        return permissionMapper.getMenuByUserId(id,permissionId);
    }

    @Override
    public Permission getMenuById(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("permission_id",id);
        Permission permission = permissionMapper.selectOne(queryWrapper);
        return permission;
    }

    @Override
    public List<Integer> getFaththIdListByid(Integer id) {
        Permission menuById = getMenuById(id);
        return getFaththIdList(menuById,new ArrayList<>());
    }
    public List<Integer> getFaththIdList(Permission permission,ArrayList<Integer> data){
        if(permission.getFatherId() == 0){
            return data;
        }
        data.add(0,permission.getFatherId());
        Permission parentPermission = getMenuById(permission.getFatherId());
        return getFaththIdList(parentPermission,data);
    }

    @Override
    public Integer addmenu(Permission permission) {
        int insert = permissionMapper.insert(permission);
        return insert;
    }

    @Override
    public Integer updateMenu(Permission permission) {
        int i = permissionMapper.updateById(permission);
        return i;
    }
    @Override
    public Integer deleteMenuById(Integer id) {
        rolePermissionMapper.delete(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getPermissionId,id));
        int i = permissionMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer deleteMenuByIdList(List id) {
        for (int i = 0; i < id.size(); i++) {
            rolePermissionMapper.delete(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getPermissionId, id.get(i)));
        }
        int i = permissionMapper.deleteBatchIds(id);
        return i;
    }
}




